package com.qcby.demo.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.qcby.demo.base.dto.DemoDTO;
import com.qcby.demo.web.entity.param.DemoParam;
import com.qcby.demo.web.entity.vo.DemoVO;
import com.qcby.framework.common.exception.ServiceException;
import com.qcby.framework.common.pojo.PageParam;
import com.qcby.framework.common.pojo.PageResult;
import com.qcby.framework.common.pojo.Result;
import com.qcby.demo.client.TestFacade;
import com.qcby.demo.client.dto.TestDTO;
import com.qcby.demo.web.exchange.AutoHandExchange;

import com.qcby.demo.base.service.ITestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;


/**
 * 测试控制器
 *
 * @author cong.zhen
 * @date 2023/04/11
 */
@RestController
@RequestMapping("test-system/exam")
@Slf4j
public class TestController {

    /**
     * 考试时长，单位毫秒
     */
    @Value("${test.time}")
    private Long examTime;

    @DubboReference(url = "dubbo://10.8.0.138:20880")
    private TestFacade testFacade;

    @Resource
    private ITestService testService;

    @PostMapping("/start-test")
    public Result<Boolean> startTest(@RequestBody DemoParam demoParam){
        DemoDTO demoDTO = BeanUtil.copyProperties(demoParam,DemoDTO.class);
        //只要开始考试，就从延时队列里放一条数据
        log.info("配置的考试时长为:{}",examTime);
        demoDTO.setCreateTime(new Date());
        demoDTO.setTestTime(examTime);
        AutoHandExchange.getDelayQueue().add(demoDTO);
        return Result.getSuccessResult(Boolean.TRUE);
    }


    @GetMapping("/page-query")
    public Result<PageResult<DemoVO>> pageQuery(DemoParam demoParam, PageParam pageParam){
        DemoDTO demoDTO = BeanUtil.copyProperties(demoParam,DemoDTO.class);
        Result<PageResult<DemoDTO>> pageResultResult = testService.demoPageQuery(demoDTO, pageParam);
        /**dto转vo**/
        List<DemoVO> demoVOS = BeanUtil.copyToList(pageResultResult.getData().getList(), DemoVO.class);
        /**构造返回对象**/
        PageResult<DemoVO> pageResult = new PageResult();
        pageResult.setTotal(pageResultResult.getData().getTotal());
        pageResult.setList(demoVOS);
        return Result.getSuccessResult(pageResult);
    }

    @GetMapping("/person-detail")
    public Result<DemoVO> getDetail(@NotNull Long id){
        DemoDTO demoById = testService.getDemoById(id);
        return Result.getSuccessResult(BeanUtil.copyProperties(demoById,DemoVO.class));
    }

    @PostMapping("/edit-person")
    public Result<Boolean> editById(@RequestBody DemoParam demoParam){
        /**参数校验**/
        if(ObjectUtil.isNull(demoParam.getId())){
            throw new ServiceException("500","id不能为空");
        }
        DemoDTO demoDTO = BeanUtil.copyProperties(demoParam,DemoDTO.class);
        return Result.getSuccessResult(testService.saveOrUpdate(demoDTO));
    }

    @PostMapping("/save-person")
    public Result<Boolean> savePerson(@RequestBody DemoParam demoParam){
        if(ObjectUtil.isNotNull(demoParam.getId())){
            throw new ServiceException("500","参数有误");
        }
        DemoDTO demoDTO = BeanUtil.copyProperties(demoParam,DemoDTO.class);
        return Result.getSuccessResult(testService.saveOrUpdate(demoDTO));
    }

    @GetMapping("remove-person")
    public Result<Boolean> removeById(@NotNull Long id){
        return Result.getSuccessResult(testService.removeById(id));
    }


    @GetMapping("/dubbo-test")
    public Result<Long> testDubbo(){
        TestDTO testById = testFacade.getTestById(1L);
        return Result.getSuccessResult(testById.getId());
    }




}
